﻿using AspNetCoreRateLimit;
using EasyCore.App;
using EasyCore.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyAdmin.Web.Extensions
{
    public static class IpRateLimitExtensions
    {
        public static void AddIpRateLimit(this IServiceCollection services)
        {
            services.AddMemoryCache();
            
            services.Configure<IpRateLimitOptions>(EasyApp.Configuration.GetSection("IpRateLimiting"));

            services.Configure<IpRateLimitPolicies>(EasyApp.Configuration.GetSection("IpRateLimitPolicies"));

            //services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
            //services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
            //services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();

            services.AddInMemoryRateLimiting();

            // the clientId/clientIp resolvers use it.
            // services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
        }

        public static void UseIpRateLimit(this IApplicationBuilder app)
        {
            app.UseIpRateLimiting();
        }
    }
}
